﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ortodoctor.gridComponent;

namespace Ortodoctor.gridComponent
{
    public partial class InsereGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("teste", typeof(System.Int32));
            dt.Columns.Add("teste2");
            dt.Columns.Add("teste3");
            dt.Columns.Add("teste4");
            dt.Columns.Add("teste5");


            for (int i = 0; i < 100000; ++i)
            {
                DataRow dr = dt.NewRow();
                int j = 1;
                foreach (DataColumn dc in dt.Columns)
                    dr[dc.ColumnName] = i * (j++);

                dt.Rows.Add(dr);
            }

            Grid grid = new Grid(dt, "teste");
            Session["GRID"] = grid;
        }

        /*
            Parâmetros	        Descrição
         
            page 	            Número da página atual.
            rp 	                O número de registros a serem retornados. 
            sortname 	        O nome da coluna para classificar.
            sortorder 	        A ordem de classificar por: 'asc' ou 'desc'.
            query 	            O texto usado dentro de uma pesquisa.
            qtype 	            A coluna selecionada durante "busca rápida".
         */

        [WebMethod]
        public static string getDados(string page, string rp, string sortname, string sortorder, string query, string qtype)
        {
            Grid grid = ((Grid)HttpContext.Current.Session["GRID"]);
            grid.page = Convert.ToInt32(page);
            DataTable dt = grid.dados.Clone();

            if (string.IsNullOrEmpty(query))
            {
                int numeroItensPagina = Convert.ToInt32(rp);
                int numeroPagina = Convert.ToInt32(page);
                int inicio = numeroItensPagina * (numeroPagina - 1);

                for (int i = inicio; i < inicio + numeroItensPagina && i < grid.dados.Rows.Count; ++i)
                {
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn dc in dt.Columns)
                        dr[dc.ColumnName] = grid.dados.Rows[i][dc.ColumnName];

                    dt.Rows.Add(dr);
                }

                dt.DefaultView.Sort = sortname + " " + sortorder;
                dt = dt.DefaultView.ToTable();
            }
            else
            {
                dt = grid.dados.Copy();
                dt.DefaultView.RowFilter = string.Format("[{0}] like '%{1}%'", qtype,query);
                dt = dt.DefaultView.ToTable();
            }
            return grid.getDadosToJson(dt);
        }

        [WebMethod]
        public static string addToTable(string dados)
        {
            Grid grid = ((Grid)HttpContext.Current.Session["GRID"]);

            HttpContext.Current.Session["GRID"] = grid;
            return "adicionou";
        }

        [WebMethod]
        public static string editTable(int id, string dados)
        {
            Grid grid = ((Grid)HttpContext.Current.Session["GRID"]);
            grid.editTable(id, dados.Split('|'));
            HttpContext.Current.Session["GRID"] = grid;
            return "editou";
        }
        
        [WebMethod]
        public static string deleteFromTable(int id)
        {
            Grid grid = ((Grid)HttpContext.Current.Session["GRID"]);
            grid.deleteFromTable(id);
            HttpContext.Current.Session["GRID"] = grid;
            return "removeu";
        }
    }
}